package com.hoshiibuy.store.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.store.entity.StoreGoodsCategory;
import com.hoshiibuy.store.entity.param.StoreGoodsCategoryQueryParam;
import com.hoshiibuy.store.entity.param.StoreGoodsCategoryUpdateParam;
import com.hoshiibuy.store.entity.param.StoreGoodsCategoryAddParam;
import com.hoshiibuy.store.service.StoreGoodsCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.hoshiibuy.common.utils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 店铺经营类目 控制器
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-04-28
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/store/store-goods-category")
@Api(value = "店铺管理", tags = {"店铺经营类目管理"})
public class StoreGoodsCategoryController {
    @Autowired
    private StoreGoodsCategoryService storeGoodsCategoryService;

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 分页查询店铺经营类目
    */
    @ApiOperation(value = "分页查询店铺经营类目")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid StoreGoodsCategoryQueryParam param) {
        try {
            log.info("分页查询店铺经营类目");
            QueryWrapper<StoreGoodsCategory> queryWrapper = new QueryWrapper<StoreGoodsCategory>();
            queryWrapper.eq(param.getStoreId() != null, "store_id", param.getStoreId());
            queryWrapper.eq(param.getCategoryId() != null, "category_id", param.getCategoryId());
            queryWrapper.eq(param.getWeight() != null, "weight", param.getWeight());
            queryWrapper.eq(param.getOrdered() != null, "ordered", param.getOrdered());
            queryWrapper.eq(param.getEnabled() != null, "enabled", param.getEnabled());
            IPage<StoreGoodsCategory> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            return Response.data(storeGoodsCategoryService.page(page, queryWrapper));
          } catch (ServiceException e) {
            return Response.failed(e.getError().getMsg());
          } catch (Exception e) {
            log.error("分页查询店铺经营类目异常", e);
            return Response.failed("分页查询店铺经营类目异常");
        }
    }
    /**
    * @param id
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 根据id查询店铺经营类目
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询店铺经营类目")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(storeGoodsCategoryService.getById(id));
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 新增
    */
    @PostMapping("/save")
    @ApiOperation(value = "新增店铺经营类目")
    public Response save(@RequestBody @Valid StoreGoodsCategoryAddParam param) {
        try {
            StoreGoodsCategory data = BeanUtils.covertType(param, StoreGoodsCategory.class);
            return Response.data(storeGoodsCategoryService.save(data));
        } catch (Exception e) {
            log.error("新增店铺经营类目异常", e);
            return Response.failed();
        }
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 修改店铺经营类目
    */
    @PutMapping("/update")
    @ApiOperation(value = "修改店铺经营类目")
    public Response update(@RequestBody @Valid StoreGoodsCategoryUpdateParam param) {
        try {
            StoreGoodsCategory data = BeanUtils.covertType(param, StoreGoodsCategory.class);
            return Response.data(storeGoodsCategoryService.updateById(data));
        } catch (Exception e) {
            log.error("修改店铺经营类目异常", e);
            return Response.failed();
        }
    }
}
